﻿Imports System.Data
Imports System.Data.SqlClient
Imports FreeTextBoxControls
Partial Class nCauhoiKS
    Inherits System.Web.UI.Page

#Region " Web Form Designer Generated Code "

    'This call is required by the Web Form Designer.
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

    End Sub



    'NOTE: The following placeholder declaration is required by the Web Form Designer.
    'Do not delete or move it.
    Private designerPlaceholderDeclaration As System.Object

    Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
        'CODEGEN: This method call is required by the Web Form Designer
        'Do not modify it using the code editor.
        InitializeComponent()
    End Sub

#End Region
    Dim blnAddNew As Boolean
    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'Put user code to initialize the page here
        Dim intID As Int16
        If Not Page.IsPostBack Then
            hideText.Text = -1
            Button1.Text = "Thêm mới"
            blnAddNew = True
            If IsNumeric(Request.QueryString("id")) Then
                intID = Request.QueryString("id")
                BindGrid(intID)
                blnAddNew = False
                Button1.Text = "Cập nhật"
            End If
        End If
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        On Error GoTo herr
        Dim sSql As String, sTraloi() As String
        Dim sIn1$, sIn2$, i%
        If Not IsNumeric(Request.QueryString("id")) Then
            For i = 1 To txtSL.Text
                sIn1 += ",luachon" & i.ToString
                sIn2 += ",@luachon" & i.ToString
            Next
            sSql = "insert into cauhoiks (cauhoi,soluachon,chonnhieu,canhan,phude,socot" & sIn1 & ") " & _
                    "values(@cauhoi,@soluachon,@chonnhieu,@canhan,@phude,@socot " & sIn2 & ")"

        Else
            For i = 1 To txtSL.Text
                sIn1 += ",luachon" & i.ToString & "=@luachon" & i.ToString
            Next

            sSql = "update cauhoiks set" & _
            " cauhoi=@cauhoi,soluachon=@soluachon,chonnhieu=@chonnhieu,canhan=@canhan,phude=@phude,socot=@socot" & _
            sIn1 & _
            " where idcauhoi=@idcauhoi"
        End If
        Dim myConn As New SqlConnection(ConfigurationSettings.AppSettings("cons"))
        Dim MyCommand As SqlCommand
        myConn.Open()
        MyCommand = New SqlCommand(sSql, myConn)
        If IsNumeric(Request.QueryString("id")) Then
            MyCommand.Parameters.Add(New SqlParameter("@idcauhoi", hideText.Text))
        End If
        MyCommand.Parameters.Add(New SqlParameter("@cauhoi", cauhoi.Text))
        MyCommand.Parameters.Add(New SqlParameter("@phude", phude.Text))
        MyCommand.Parameters.Add(New SqlParameter("@socot", socot.Text))
        MyCommand.Parameters.Add(New SqlParameter("@soluachon", txtSL.Text))
        MyCommand.Parameters.Add(New SqlParameter("@chonnhieu", chkChonnhieu.Checked))
        MyCommand.Parameters.Add(New SqlParameter("@canhan", chkTTCanhan.Checked))
        sTraloi = Split(TextBox1.Text, vbCrLf)
        For i = 1 To txtSL.Text
            MyCommand.Parameters.Add(New SqlParameter("@luachon" & i.ToString, sTraloi(i - 1)))
        Next
        MyCommand.ExecuteNonQuery()
        myConn.Close()
        Response.Redirect("nCauhoiks.aspx")
        Exit Sub
herr:
        Label1.Text = Err.Description
    End Sub
    Private Sub BindGrid(ByVal vId As Int16)
        On Error Resume Next
        Dim dr As DataRow
        Dim myConn As New SqlConnection(ConfigurationSettings.AppSettings("cons"))
        Dim sSql As String = "select * from cauhoiks where idcauhoi = " & vId
        Dim strTraloi As String
        myConn.Open()
        Dim myComm As New SqlCommand(sSql, myConn)
        Dim myDr As SqlDataReader
        myDr = myComm.ExecuteReader
        If myDr.Read Then
            Dim i%
            cauhoi.Text = myDr("cauhoi")
            strTraloi = ""
            For i = 1 To myDr("soluachon")
                strTraloi += myDr("luachon" & i.ToString) & vbCrLf
            Next
            TextBox1.Text = strTraloi
            hideText.Text = myDr("idcauhoi")
            txtSL.Text = myDr("soluachon")
            chkChonnhieu.Checked = myDr("chonnhieu")
            chkTTCanhan.Checked = myDr("canhan")
            phude.Text = myDr("phude")
            socot.Text = myDr("socot")
        End If
        myConn.Close()
    End Sub

End Class


